#!/bin/bash

cd /batocera/batocera.linux || exit 1

SEARCHDIR="./run/current"
LOGDIR="./run/logs"
ACTIONSDIR="./run/actions"

export BATCH_MODE=1

init() {
    mkdir -p run/current || return 1
    mkdir -p run/logs    || return 1
    mkdir -p run/actions || return 1
    mkdir -p buildroot-ccache || return 1
}

echo_msg() {
    arch=$1
    echo $(date "+%Y/%m/%d %H:%M:%S") "[${arch}]" $2
}

do_push() {
    ftptarget=$1
    arch=$2

    BOARDS=$(ls "output/${arch}/images/batocera/images")

    for board in ${BOARDS}
    do
	# hack during the time the rpi4 is 32 bits
	remoteboarddir=${board}
	test "${board}" = rpi4 && remoteboarddir=rpi464

	case "${ftptarget}" in
	    "butterfly")
		echo_msg "${arch}" "pushing ${board} on ${ftptarget}..."
		rsync -a --delete "output/${arch}/images/batocera/images/${board}/" "batocera@mirrors.o2switch.fr:/opt/www/batocera/${remoteboarddir}/butterfly/last/"
		;;
	    "rc")
		# pushing butterfly
		echo_msg "${arch}" "pushing ${board} on butterfly*..."
		rsync -a --delete "output/${arch}/images/batocera/images/${board}/" "batocera@mirrors.o2switch.fr:/opt/www/batocera/${remoteboarddir}/butterfly/last/"

		# pushing rc
		echo_msg "${arch}" "pushing ${board} on ${ftptarget}..."
		rsync -a --delete "output/${arch}/images/batocera/images/${board}/" "batocera@mirrors.o2switch.fr:/opt/www/batocera/${remoteboarddir}/stable/rc/"
		;;
	    "unstable")
		echo_msg "${arch}" "pushing ${board} on ${ftptarget}..."
		rsync -a --delete "output/${arch}/images/batocera/images/${board}/" "batocera@mirrors.o2switch.fr:/opt/www/batocera/${remoteboarddir}/unstable/last/"
		;;
	    *)
		echo_msg "${arch}" "invalid ftp target" >&2
		return 1
	esac
    done

    # update the butterfly file
    ssh batocera@mirrors.o2switch.fr /opt/www/batocera/listBetas.sh
}

do_html() {
    arch=$1

    BOARDS=$(ls "output/${arch}/images/batocera/images")

    for board in ${BOARDS}
    do
        mkdir -p "output/${arch}/images/batocera/images/${board}/" || return 1
	cp "output/${arch}/images/batocera/batocera_systemsReport.html" "output/${arch}/images/batocera/images/${board}/" || return 1
	cp "output/${arch}/images/batocera/batocera_systemsReport.json" "output/${arch}/images/batocera/images/${board}/" || return 1
	(docker run --rm --name "${arch}_html_"$(date "+%Y%m%d_%H%M%S") -v $PWD:/build batoceralinux/batocera.linux-build /bin/bash -c "/build/scripts/linux/buildstats.sh /build/output/${arch} ${board} > /build/output/${arch}/images/batocera/images/${board}/index.html")
    done
}

# wakeonlan b4:b5:2f:cb:74:07
do_suspend() {
    sudo systemctl suspend
}

checkFtpTarget() {
    ftptarget=$1
    if test "${ftptarget}" != "butterfly" -a "${ftptarget}" != "rc" -a "${ftptarget}" != "unstable"
    then
	echo_msg "-" "invalid ftp target ${ftptarget}" >&2
	return 1
    fi
    return 0
}

buildAll() {
    ftptarget=$1

    touch "${ACTIONSDIR}/suspend" || return 1
    while true
    do
	NEXTITEM=$(cd "${SEARCHDIR}" && ls -1t | head -1)
	if test -z "${NEXTITEM}"
	then
	    echo_msg "-" "Ended."
	    if test -e "${ACTIONSDIR}/suspend"
	    then
		do_suspend
	    fi
	    return 0
	fi
	rm -f "${SEARCHDIR}/${NEXTITEM}"

	# cleaning system and splash package for timestamp regeneration
	echo_msg "${NEXTITEM}" "removing build/batocera-system-* and build/batocera-splash-*"
	if ! rm -rf "output/${arch}/build/batocera-system-"* "output/${arch}/build/batocera-splash-"* 2>/dev/null
	then
	    return 1
	fi
	echo_msg "${NEXTITEM}" "building..."
	if make "${NEXTITEM}-build" > "${LOGDIR}/build-${NEXTITEM}.log" 2> "${LOGDIR}/build-${NEXTITEM}.err"
	then
	    do_html "${NEXTITEM}" "${ftptarget}"

	    if test -n "${ftptarget}"
	    then
		if do_push "${ftptarget}" "${NEXTITEM}"
		then
		    echo_msg "${NEXTITEM}" "pushed"
		else
		    echo_msg "${NEXTITEM}" "unable to push"
		fi
	    fi
	else
	    echo_msg "${NEXTITEM}" "failed!"
	fi
    done
}

case "${1}" in
    "buildfg")
	ftptarget=$2
	if test -n "${ftptarget}"
	then
	    checkFtpTarget "${ftptarget}" || exit 1
	fi
	buildAll "${ftptarget}" || exit 1
	;;
    "build")
	ftptarget=$2
	init || exit 1
	if test -n "${ftptarget}"
	then
	    checkFtpTarget "${ftptarget}" || exit 1
	fi
	nohup "${0}" buildfg "${ftptarget}" > "${LOGDIR}/run.log" 2>&1 &
	exit 0
	;;
    "enter")
	arch="${2}"
	unset BATCH_MODE
	init || exit 1
	make "${arch}-shell" || exit 1
	;;
    "defconfig")
	arch="${2}"
	make "${arch}-config" || exit 1
	;;
    "add")
	init || exit 1
	while test -n "${2}"
	do
	    arch="${2}"
	    shift
	    touch "${SEARCHDIR}/${arch}"
	done
	;;
    "list")
	init || exit 1
	ls -1t "${SEARCHDIR}"
	;;
    "log")
	if test -n "${2}"
	then
	    arch="${2}"
	    tail -f "${LOGDIR}/build-${arch}.log"
	else
	    tail -f "${LOGDIR}/run.log"
	fi
	;;
    "logpkg")
	if test -z "${2}"
	then
	    echo_msg "-" "missing argument" >&2
	    exit 1
	fi
	arch="${2}"
	tail -f "./output/${arch}/build/build-time.log"
	;;
    "push")
	ftptarget=${2}
	checkFtpTarget "${ftptarget}" || exit 1
	shift
	shift
	while test -n "${1}"
	do
	    arch="${1}"
	    shift
	    do_push "${ftptarget}" "${arch}" || exit 1
	done
	;;
    "html")
	shift
	arch="${1}"
	do_html "${arch}" || exit 1
	;;

    "suspend")
	do_suspend
	;;

    *)
	PROGNAME=$(basename "${0}")
	echo "${PROGNAME} defconfig x86"
	echo "${PROGNAME} build butterfly"
	echo "${PROGNAME} build rc"
	echo "${PROGNAME} build unstable"
	echo "${PROGNAME} enter x86"
	echo "${PROGNAME} add x86"
	echo "${PROGNAME} list"
	echo "${PROGNAME} log"
	echo "${PROGNAME} log x86"
	echo "${PROGNAME} logpkg x86"
	echo "${PROGNAME} push butterfly x86 bcm2837 bcm2835"
	echo "${PROGNAME} push rc x86 bcm2837 bcm2835"
	echo "${PROGNAME} suspend"
esac

exit 0
